﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KipmeHQ;

public partial class Orders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected override void OnPreRender(EventArgs e)
    {
        ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "DefineShow", "var element = $(':radio:checked').val(); if (element == 'By product code') Byproductcode(); if (element == 'By date') Bydate();", true);
        ScriptManager.RegisterClientScriptBlock(UpdatePanel2, UpdatePanel2.GetType(), "Bydate", "function Bydate() { var elements = $('.TransactionProduct').css('display', 'none'); var elements = $('.TransactionDate').css('display', 'block'); } ", true);
        ScriptManager.RegisterClientScriptBlock(UpdatePanel2, UpdatePanel2.GetType(), "Byproductcode", "function Byproductcode() { var elements = $('.TransactionProduct').css('display', 'block'); var elements = $('.TransactionDate').css('display', 'none');}", true);
        ScriptManager.RegisterClientScriptBlock(UpdatePanel2, UpdatePanel2.GetType(), "DefineShowF", "function DefineShow() {var element = $(':radio:checked').val(); if (element == 'By product code') Byproductcode(); if (element == 'By date') Bydate();}", true);


        if (!Page.IsPostBack)
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            ddlDepartments.DataSource = service.getAllDepartments();
            ddlDepartments.DataValueField = "id";
            ddlDepartments.DataTextField = "name";
            ddlDepartments.DataBind();
        }
        base.OnPreRender(e);
    }

    protected void lvTransactions_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
    {
        HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
        if (service == null)
        {
            service = new HeadquarterWSClient();
        }
        lvTransactions.DataSource = service.getDepartmentProductsInfo();
        lvTransactions.SelectedIndex = e.NewSelectedIndex;
        lvTransactions.DataBind();
    }

    protected void btnGetTransactionsByProduct_Click(object sender, EventArgs e)
    {
        HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
        if (service == null)
        {
            service = new HeadquarterWSClient();
        }
        try
        {
            gvTransactions.DataSource = service.getDepartmentTransactionsByCode(lvTransactions.SelectedValue.ToString());
            gvTransactions.DataBind();
        }
        catch (System.Exception ex)
        {
        }
    }
    protected void btnGetTransactionsByDate_Click(object sender, EventArgs e)
    {
        HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
        if (service == null)
        {
            service = new HeadquarterWSClient();
        }
        if (StartCalendar.SelectedDate > EndCalendar.SelectedDate)
            return;
        gvTransactions.DataSource = service.getDepartmentTransactionsByDate(StartCalendar.SelectedDate.ToString(), EndCalendar.SelectedDate.ToString());
        gvTransactions.DataBind();
    }

    protected void btnProceed_Click(object sender, EventArgs e)
    {
        HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
        if (service == null)
        {
            service = new HeadquarterWSClient();
        }
        service.setFocusOnDepartment(int.Parse(ddlDepartments.SelectedValue));
        lvTransactions.DataSource = service.getDepartmentProductsInfo();
        lvTransactions.SelectedIndex = -1;
        lvTransactions.DataBind();
        TransactionDetails.Style.Add("display", "marker");
    }
}